﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SecurePad.Properties;
using System.IO;
using System.Reflection;

namespace SecurePad
{
    public partial class FormMain : Form
    {
        private SecurePadFile securePadFile;
        private bool isModified;

        public FormMain()
        {
            this.InitializeComponent();
            this.FormClosing += (sender, e) =>
            {
                if (this.WindowState == FormWindowState.Normal)
                {
                    Settings.Default.WindowSize = this.Size;
                }
                else
                {
                    Settings.Default.WindowSize = this.RestoreBounds.Size;
                }

                Settings.Default.Save();
            };

            this.Size = Settings.Default.WindowSize;
            this.txtContent.WordWrap = Settings.Default.WordWrap;
            this.wordWrapToolStripMenuItem.Checked = Settings.Default.WordWrap;

            this.Load += (sender, e) =>
            {
                this.isModified = false;

                if (this.securePadFile == null)
                {
                    this.SetStatusFileName(null);
                }
            };
        }

        private void TextChangedEvent(object sender, EventArgs e)
        {
            this.isModified = true;
        }

        private void CutEvent(object sender, EventArgs e)
        {
            this.txtContent.Cut();
        }

        private void CopyEvent(object sender, EventArgs e)
        {
            this.txtContent.Copy();
        }

        private void PasteEvent(object sender, EventArgs e)
        {
            this.txtContent.Paste();
        }

        private void UndoEvent(object sender, EventArgs e)
        {
            this.txtContent.Undo();
        }

        private void RedoEvent(object sender, EventArgs e)
        {
            this.txtContent.Redo();
        }

        private void NewEvent(object sender, EventArgs e)
        {
            this.securePadFile = null;
            this.txtContent.Text = string.Empty;
            this.SetStatusFileName(null);
            this.isModified = false;
        }

        private void OpenEvent(object sender, EventArgs e)
        {
            if (this.openFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (File.Exists(this.openFileDialog.FileName) == false)
                {
                    return;
                }

                OpenFile(this.openFileDialog.FileName);
            }
        }

        private void SaveEvent(object sender, EventArgs e)
        {
            if (this.securePadFile == null)
            {
                SaveAsEvent(sender, e);
                return;
            }

            this.Save(this.securePadFile.FilePath);
        }

        private void SaveAsEvent(object sender, EventArgs e)
        {
            if (this.saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = this.saveFileDialog.FileName;
                Save(filePath);
            }
        }

        private void FontEvent(object sender, EventArgs e)
        {
            this.fontDialog.Font = this.txtContent.Font;
            if (this.fontDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtContent.Font = this.fontDialog.Font;
            }
        }

        private void ExitEvent(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void AboutEvent(object sender, EventArgs e)
        {
            FormAbout aboutForm = new FormAbout();
            aboutForm.ShowDialog();
        }

        private void FormClosingEvent(object sender, FormClosingEventArgs e)
        {
            if (this.isModified)
            {
                DialogResult result = MessageBox.Show(Resources.SaveChanges, Resources.SaveChangesTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (result == DialogResult.Yes)
                {
                    if (this.securePadFile == null)
                    {
                        SaveAsEvent(sender, EventArgs.Empty);
                        return;
                    }

                    this.Save(this.securePadFile.FilePath);
                }
            }
        }

        private void WordWrapEvent(object sender, EventArgs e)
        {
            this.txtContent.WordWrap = Settings.Default.WordWrap = wordWrapToolStripMenuItem.Checked;
        }

        private string GetNewPassword()
        {
            FormCreatePassword formPassword = new FormCreatePassword();
            if (formPassword.ShowDialog() == DialogResult.Cancel)
            {
                return null;
            }

            return formPassword.Tag as string;
        }

        private string GetPassword()
        {
            FormEnterPassword formPassword = new FormEnterPassword();
            if (formPassword.ShowDialog() == DialogResult.Cancel)
            {
                return null;
            }

            return formPassword.Tag as string;
        }

        public bool OpenFile(string filePath)
        {
            this.securePadFile = new SecurePadFile();

            string fileExtension = new FileInfo(filePath).Extension;
            if (".spd".Equals(fileExtension, StringComparison.InvariantCultureIgnoreCase))
            {
                string password = null;
                do
                {
                    password = GetPassword();
                    if (password == null)
                    {
                        return false;
                    }
                }
                while (this.securePadFile.OpenFile(filePath, password) == false);
            }
            else
            {
                this.securePadFile.OpenFile(filePath);
            }

            this.SetStatusFileName(this.securePadFile.FilePath);
            this.txtContent.Text = this.securePadFile.Content;
            this.isModified = false;

            return true;
        }

        private void Save(string filePath)
        {
            this.securePadFile = new SecurePadFile();

            string fileExtension = new FileInfo(filePath).Extension;
            if (".spd".Equals(fileExtension, StringComparison.InvariantCultureIgnoreCase))
            {
                string password = GetNewPassword();
                if (password == null)
                {
                    return;
                }

                this.securePadFile.Save(filePath, txtContent.Text, password);
            }
            else
            {
                this.securePadFile.Save(filePath, txtContent.Text);
            }

            this.isModified = false;
            this.SetStatusFileName(filePath);
        }

        private void SetStatusFileName(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                this.toolStripStatusLabel.Text = string.Empty;
                this.toolStripStatusLabelIsEncrypted.Text = "[unlock]";
            }
            else
            {
                FileInfo fileInfo = new FileInfo(filePath);
                this.toolStripStatusLabel.Text = fileInfo.Name;
                if (".spd".Equals(fileInfo.Extension))
                {
                    this.toolStripStatusLabelIsEncrypted.Text = "[lock]";
                }
                else
                {
                    this.toolStripStatusLabelIsEncrypted.Text = "[unlock]";
                }
            }
        }
    }
}
